<?php
/**
 * 消息系统ACTION
 *
 * @author ananda
 */
class Action_Relation_Index extends Action
{
    /**
     * 消息系统初始时的唯一入口
     */
    public function main()
    {
        return $this->ping();
    }

    /**
     * 返回拥有的函数
     */
    public function ping()
    {
        return implode(',', get_class_methods($this));
    }

    public function getListToUser($user_id, $page = null, $limit = null)
    {
        $people = Com::glob()->people->gen($user_id, 'user');
        $key    = $this->controller->relation->generateToKey($people);
        $data   = $this->controller->relation->getList($key);
        if (Error::is($data)) {
            throw new Exception($data->getMessage());
        }
        $temp = $data->getData();
        $return['total']  = count($temp);

        if ($page && $limit) {
            $return['data'] = array();
            $keys = array_keys($temp);
            $start = (($page - 1) * $limit);
            $end = $start + $limit;
            for ($i = $start; $i < $end; $i++) {
                if (!isset($keys[$i])) {
                    break;
                }
                $return['data'][$keys[$i]] = $temp[$keys[$i]];
            }
        } else {
            $return['data'] = $temp;
        }
        return $return;
    }

    public function getListFromUser($user_id, $page = null, $limit = null)
    {
        $people = Com::glob()->people->gen($user_id, 'user');
        $key    = $this->controller->relation->generateFromKey($people);
        $data   = $this->controller->relation->getList($key);
        if (Error::is($data)) {
            throw new Exception($data->getMessage());
        }
        $temp = $data->getData();
        $return['total']  = count($temp);

        if ($page && $limit) {
            $return['data'] = array();
            $keys = array_keys($temp);
            $start = (($page - 1) * $limit);
            $end = $start + $limit;
            for ($i = $start; $i < $end; $i++) {
                if (!isset($keys[$i])) {
                    break;
                }
                $return['data'][$keys[$i]] = $temp[$keys[$i]];
            }
        } else {
            $return['data'] = $temp;
        }
        return $return;
    }

    /**
     * 用户检测两用户之间的关系
     * 待写
     * @param integer $from_user_id
     * @param integer $to_user_id
     * @return integer 0表示互不关注，1表示FROM关注TO，2表示TO关注FROM，3表示相互关注
     */
    public function checkUser($from_user_id, $to_user_id)
    {
        return 2;
    }

    public function appendUser($from_user_id, $to_user_id)
    {
        $people_from = Com::glob()->people->gen($from_user_id, 'user');
        $people_to   = Com::glob()->people->gen($to_user_id,   'user');
        $result = $this->controller->relation->append($people_from, $people_to, $from_user_id,
                                                      $to_user_id, Com::glob('time'));
        if (Error::is($result)) {
            throw new Exception($result->getMessage());
        }
        return $result;
    }

    /**
     * 取消From对To的关注
     */
    public function cancelUser($from_user_id, $to_user_id)
    {
        $people_from = Com::glob()->people->gen($from_user_id, 'user');
        $people_to   = Com::glob()->people->gen($to_user_id,   'user');
        return $this->controller->relation->cancel($people_from, $people_to,
                                                   $from_user_id, $to_user_id);
    }
}
